package utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

import org.postgresql.util.Base64;

public class PasswordUtility {

	private static PasswordUtility instance;

	private PasswordUtility() {
	}

	public static synchronized PasswordUtility getInstance() {
		if (instance == null)
			instance = new PasswordUtility();
		return instance;
	}

	public String encrypt(String password) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			// TODO: log here eventual errors
		}
		md.update(password.getBytes());
		byte byteData[] = md.digest();
		String hashedPassword = Base64.encodeBytes(byteData);
		return hashedPassword;
	}

}
